﻿/***********************************************************************
 * <copyright file="ComboGridExtension.cs" company="B/S JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   LinhMC
 * Email:    linhmc@B/S.vn
 * Website:
 * Create Date: Thursday, November 14, 2013
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

using System.Globalization;
using System.Web.Mvc;

namespace EasyUIMvc.ComboBox
{
    public class ComboGridExtension : ExtensionBase
    {
        private readonly ComboGridSettings _settings;

        public ComboGridExtension(ComboGridSettings settings)
        {
            _settings = settings;
        }

        public override string GetScripts()
        {
            throw new System.NotImplementedException();
        }

        public override MvcHtmlString GetHtml()
        {
            _settings.DataOptions += "panelWidth: '" + _settings.DataOption.PanelWidth + "',";
            _settings.DataOptions += "idField: '" + _settings.DataOption.ValueMember + "',";
            _settings.DataOptions += "textField: '" + _settings.DataOption.DisplayMember + "',";
            _settings.DataOptions += "url: '" + _settings.DataOption.UrlJsonDataSource + "',";
            _settings.DataOptions += "method: '" + _settings.DataOption.Method + "',";
            if (_settings.DataOption.Mode != null)
            {
                _settings.DataOptions += "mode: '" + _settings.DataOption.Mode + "',";
            }
            _settings.DataOptions += "columns: [[";
            for (int i = 0; i < _settings.DataOption.ColumnName.Length; i++)
            {
                _settings.DataOptions += "{field:'" + _settings.DataOption.ColumnName[i] + "', title:'" +
                                         _settings.DataOption.ColumnText[i] + "'";
                if (!string.IsNullOrEmpty(_settings.DataOption.ColumnWidth[i].ToString(CultureInfo.InvariantCulture)))
                {
                    _settings.DataOptions += ",width: " + _settings.DataOption.ColumnWidth[i];
                }
                if (!string.IsNullOrEmpty(_settings.DataOption.ColumnTextAlign[i]))
                {
                    _settings.DataOptions += ",align:' " + _settings.DataOption.ColumnTextAlign[i] + "'";
                }
                _settings.DataOptions += "}";
                if (i < _settings.DataOption.ColumnName.Length-1)
                {
                    _settings.DataOptions += ",";
                }
            }
            _settings.DataOptions += "]],";
            _settings.DataOptions += "fitColumns: " + _settings.DataOption.AutoFitComlumn.ToString().ToLower();

            if (_settings.TagName == "")
            {
                _settings.TagName = "select";
            }

            var comboGrid = GetInnerTag(_settings.TagName, "easyui-combogrid", _settings);
            if (_settings.Value != "")
            {
                comboGrid.MergeAttribute("value", _settings.Value);
            }
            comboGrid.MergeAttribute("style", _settings.Style);
            return MvcHtmlString.Create(comboGrid.ToString(_settings.TagRenderMode));
        }
    }

    public class ComboGridSettings : SettingsBase
    {
        public string TagName { get; set; }
        public TagRenderMode TagRenderMode { get; set; }
        public string Style { get; set; }
        public string Value { get; set; }
        public ComboDataOption DataOption { get; set; }
    }

    public class ComboDataOption
    {
        public short PanelWidth { get; set; }
        public string ValueMember { get; set; }
        public string DisplayMember { get; set; }
        public string UrlJsonDataSource { get; set; }
        public string Method { get; set; }
        public string Mode { get; set; } //add by thangnd- use for autocomplete
        public bool AutoFitComlumn { get; set; }
        public string[] ColumnName { get; set; }
        public string[] ColumnText { get; set; }
        public short[] ColumnWidth { get; set; }
        public string[] ColumnTextAlign { get; set; }
    }
}
